package com.zebraphp.devtool.httpUtils;

import android.content.Context;
import android.widget.ImageView;

import com.zebraphp.devtool.httpUtils.NetImageHttp.ImageRequestBuiler;
import com.zebraphp.devtool.httpUtils.NetImageHttp.LoadImageTool;
import com.zebraphp.devtool.httpUtils.NetOkHttp.IOkHttp;
import com.zebraphp.devtool.httpUtils.NetOkHttp.Interceptor.ResponseInterceptor;
import com.zebraphp.devtool.httpUtils.NetOkHttp.responseHandler.BaseHandler;
import com.zebraphp.devtool.httpUtils.NetOkHttp.responseHandler.DownloadHandler;
import com.zebraphp.devtool.httpUtils.cookieJarPersistent.PersistentCookieJar;
import com.zebraphp.devtool.httpUtils.cookieJarPersistent.cache.CookieRunCacheUtil;
import com.zebraphp.devtool.httpUtils.cookieJarPersistent.storage.CookieStoreUtil;

import java.io.File;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * Created by SimonTeam on 2017/8/12.
 * Http请求客户端 调配各种请求组件 okhttp,picasso等相关组件
 */

public class HttpClient {

    private static IOkHttp mIOkHttp = null;
    private static PersistentCookieJar mPersistentCookieJar = null;

    //清除Http session信息
    public static void clearSession(Context context) { }

    private static IOkHttp getInstance(Context context) {
        if (mIOkHttp == null) {
            //Cookie持久化管理类
            PersistentCookieJar mPersistentCookieJar = new PersistentCookieJar(new CookieRunCacheUtil(), new CookieStoreUtil(context));
            //OkHttp 配置
            OkHttpClient mOkHttpClientConfig = new OkHttpClient.Builder()
//                    .connectTimeout(10000L, TimeUnit.MILLISECONDS)
//                    .readTimeout(10000L, TimeUnit.MILLISECONDS)
//                    .writeTimeout(10000L, TimeUnit.MILLISECONDS)
                    .cookieJar(mPersistentCookieJar)//交给 自定义的PersistentCookieJar 持久化管理
                    .addInterceptor(new ResponseInterceptor())//加入拦截器
                    .build();

            mIOkHttp = new IOkHttp(mOkHttpClientConfig);
        }

        return mIOkHttp;

    }

    //Get请求
    public static void doGet(Context context, String url, BaseHandler responseParseHandler) {
        getInstance(context).genGetRequest().setUrl(url).addTaskQueue(responseParseHandler);
    }

    public static void doGetParams(Context context, String url, Map<String,String> params, BaseHandler responseParseHandler) {
        getInstance(context).genGetRequest().setUrl(url).setParams(params).addTaskQueue(responseParseHandler);
    }

    //Post请求
    public static void doPost(Context context, String url, Map<String, String> params, BaseHandler responseParseHandler) {
        getInstance(context).genPostRequest().setUrl(url).setParams(params).addTaskQueue(responseParseHandler);
    }

    //显示图片
    public static void showImage(ImageView iv, String url) {
        ImageRequestBuiler request = new ImageRequestBuiler.Builder().url(url).imgView(iv).create();
        LoadImageTool.getProvider().loadImage(request);//采用第三方的毕加索类库，加载展示图片
    }

    //下载单个文件
    public static void downloadFile(Context context, String downloadUrl, String savePath, DownloadHandler responseParseHandler) {
        getInstance(context).genDownloadRequest().setUrl(downloadUrl).setFilePath(savePath).addTaskQueue(responseParseHandler);
    }

    //上传单个文件
    public static void uploadFile(Context context, String url, Map<String, String> params, File file, BaseHandler responseParseHandler) {
        getInstance(context).genUploadRequest().setUrl(url).setParams(params).addFile("file", file).addTaskQueue(responseParseHandler);

    }

    //上传多个文件
    public static void uploadFiles(Context context, String url, Map<String, String> params, Map<String, File> files, BaseHandler responseParseHandler) {
        getInstance(context).genUploadRequest().setUrl(url).setParams(params).files(files).addTaskQueue(responseParseHandler);
    }

}
